package com.zhao.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.pojo.Chat;
import com.zhao.pojo.Follow;
import com.zhao.pojo.User;
import com.zhao.pojo.comm.ChatList;
import com.zhao.service.ChatService;
import com.zhao.service.FollowService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Chat")
public class ChatController {

    @Autowired
    FollowService followService;
    @Autowired
    ChatService chatService;

    @RequestMapping("/toChat/{uid}")
    public String ToChat(@PathVariable(required = false) String uid,Model model){
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        if (loginUser==null){
            return "login";
        }
        Integer id=loginUser.getUserId();
        //获取 当前用户的关注列表
        List<ChatList> chatList=followService.followList(id);
        for (ChatList chatList1:chatList) {
            //获取聊天记录
            List<Chat> chats = chatService.list(new QueryWrapper<Chat>()
                    .eq("uid",id)
                    .eq("cid",chatList1.getFid())
                    .or()
                    .eq("uid",chatList1.getFid())
                    .eq("cid",id)
                    .orderByAsc("create_time")
                    .last("limit 50"));
            //获取最后一条消息
            if (chats != null && chats.size() != 0){
                Chat chat=chats.get(chats.size() - 1);
                String content = chat.getContent();
                if (content.length()>10){
                    //取出表情 剩余的文字进行判断
                    if (content.contains("border=\"0\"  width=\"25\" height=\"25\"")){
                        content=content.substring(0,content.indexOf("height=\"25\">")+12)+"...";
                       // System.out.println("================>"+content);
                    }else {
                        content=content.substring(0,10)+"...";
                    }
                }
                List<Chat> looks = chatService.list(new QueryWrapper<Chat>()
                        .eq("uid",chatList1.getFid())
                        .eq("cid",id)
                        .eq("look","0")
                        .orderByAsc("create_time"));
                System.out.println("==================>"+(looks.size()==0));
                if(looks.size()==0){
                    chatList1.setLook("1");
                }else {
                    chatList1.setLook("0");
                }


                chatList1.setTime(chat.getCreateTime());//最后联系时间
                chatList1.setPreview(content);//最后一条记录
                chatList1.setChats(chats);//全部记录
            }
        }
        model.addAttribute("chatList",chatList);
        model.addAttribute("id",id);
        model.addAttribute("uid",uid);
        return "admin/chat";
    }
    //获取 当前用户的关注列表
    //获取聊天记录
    @ResponseBody
    @RequestMapping("/chatList")
    public String chatList(Integer id){
        //获取 当前用户的关注列表
        List<ChatList> chatList=followService.followList(id);
        for (ChatList chatList1:chatList) {
            //获取聊天记录
            List<Chat> chats = chatService.list(new QueryWrapper<Chat>()
                    .eq("uid",id)
                    .eq("cid",chatList1.getFid())
                    .or()
                    .eq("uid",chatList1.getFid())
                    .eq("cid",id)
                    .orderByAsc("create_time"));
            //获取最后一条消息
            if (chats != null && chats.size() != 0){
                Chat chat=chats.get(chats.size() - 1);
                String content = chat.getContent();
                if (content.length()>10){
                    content=content.substring(0,9)+"...";
                }
                chatList1.setLook(chat.getLook()+"");
                chatList1.setTime(chat.getCreateTime());//最后联系时间
                chatList1.setPreview(content);//最后一条记录
                chatList1.setChats(chats);//全部记录
            }else {
                chatList1.setLook("1");
                chatList1.setTime(null);//最后联系时间
                chatList1.setPreview("");//最后一条记录
                chatList1.setChats(null);
            }
        }
        return JSONObject.toJSONString(chatList);
    }

    //已读
    @ResponseBody
    @RequestMapping("/read/{id}")
    public String read(@PathVariable String id){
        System.out.println(id);
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        List<Chat> list = chatService.list(new QueryWrapper<Chat>().eq("look", 0).eq("uid", id).eq("cid", loginUser.getUserId()));
        if (list.size()!=0){
            for (Chat chat:list) {
                chat.setLook(1);
            }
            chatService.updateBatchById(list);
        }
        return "1";
    }

    //发送消息
    @ResponseBody
    @RequestMapping("/send")
    public String send(Chat chat){
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        chat.setUid(loginUser.getUserId());
        chatService.save(chat);
        return "1";
    }

    /*//搜索好友

    @RequestMapping("/query/{account}")
    public String query(@PathVariable String account){
        //查询这个的id
        return "";
    }*/

}
